package com.base.controller;

import com.base.po.voip.SelfShopBrowse;
import com.base.po.voip.Users;
import com.base.service.SelfShopBrowseService;
import com.base.service.UsersService;
import com.base.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 自营商城浏览记录
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/admin")
public class index_SelfShopBrowse {
    @Autowired
    private UsersService usersService;
    @Autowired
    private SelfShopBrowseService selfShopBrowseService;

    // 自营商城订单首页
    @RequestMapping("/selfShopBrowse")
    public ModelAndView index(HttpServletRequest request) throws Exception {
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
        Page page = new Page();
        Users users = usersService.selectByPrimaryKey(adminUserId);
        // 浏览记录
        List<SelfShopBrowse> selfShopBrowse = null;
        //累计浏览
        int allBrowse = 0;
        page.setCurrentPageNo(1);
        if (users.getRole() == 0) {
            allBrowse = selfShopBrowseService.countByParentID(null, null);
            page.setRecordTotal(allBrowse);
            selfShopBrowse = selfShopBrowseService.selectByParentID(page, null, null);
        }

        if (users.getRole() == 1) {
            allBrowse = selfShopBrowseService.countByParentID(adminUserId, adminUserId);
            page.setRecordTotal(allBrowse);
            selfShopBrowse = selfShopBrowseService.selectByParentID(page, adminUserId, adminUserId);
            allBrowse = allBrowse + users.getBrowse();
        }

        if (users.getRole() == 2) {
            allBrowse = selfShopBrowseService.countByParentID(users.getParentid(), adminUserId);
            page.setRecordTotal(allBrowse);
            selfShopBrowse = selfShopBrowseService.selectByParentID(page, users.getParentid(), adminUserId);
            Users pUser = usersService.selectByPrimaryKey(users.getParentid());
            allBrowse = allBrowse + pUser.getBrowse();
        }

        request.setAttribute("totlenum", page.getRecordTotal()); // 分页
        request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中

        request.setAttribute("allBrowse", allBrowse);
        request.setAttribute("selfShopBrowse", selfShopBrowse);
        request.setAttribute("adminUsergrade", adminUsergrade);
        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_SelfShopBrowse");
        return modelAndView;
    }

    @RequestMapping("/selectSelfBrowse")
    public ModelAndView selectSelfBrowse(HttpServletRequest request, Integer curPage) {
        // 设置session缓存
        if (curPage == null) {
            if (request.getSession().getAttribute("curPage") != null) {
                curPage = (Integer) request.getSession().getAttribute("curPage");
            } else {
                curPage = 1;
            }
        }
        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");

        ModelAndView modelAndView = new ModelAndView();
        Page page = new Page();
        Users users = usersService.selectByPrimaryKey(adminUserId);

        //已有商品
        List<SelfShopBrowse> selfShopBrowse = null;
        //累计浏览
        int allBrowse = 0;
        page.setCurrentPageNo(curPage);
        if (users.getRole() == 0) {
            allBrowse = selfShopBrowseService.countByParentID(null, null);
            page.setRecordTotal(allBrowse);
            selfShopBrowse = selfShopBrowseService.selectByParentID(page, null, null);
        }

        if (users.getRole() == 1) {
            allBrowse = selfShopBrowseService.countByParentID(adminUserId, adminUserId);
            page.setRecordTotal(allBrowse);
            selfShopBrowse = selfShopBrowseService.selectByParentID(page, adminUserId, adminUserId);
            allBrowse = allBrowse + users.getBrowse();
        }

        if (users.getRole() == 2) {
            allBrowse = selfShopBrowseService.countByParentID(users.getParentid(), adminUserId);
            page.setRecordTotal(allBrowse);
            selfShopBrowse = selfShopBrowseService.selectByParentID(page, users.getParentid(), adminUserId);
            Users pUser = usersService.selectByPrimaryKey(users.getParentid());
            allBrowse = allBrowse + pUser.getBrowse();
        }

        request.setAttribute("totlenum", page.getRecordTotal()); // 分页
        request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中

        request.setAttribute("allBrowse", allBrowse);
        request.setAttribute("selfShopBrowse", selfShopBrowse);
        request.setAttribute("adminUsergrade", adminUsergrade);
        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_SelfShopBrowse");
        return modelAndView;
    }
}
